<?php if (!defined('BASEPATH')) exit('No direct script access allowed');

/**
 * MY_Table
 *
 * @author Simon Emms <simon@simonemms.com>
 */
class MY_Table extends CI_Table {
    
    protected $_objCI;

    protected $_translate = false;
    
    
    
    /**
     * Construct
     *
     * If a table is generated, it calls the table CSS
     * file.
     */
    public function  __construct() {
        parent::__construct();

        $this->_objCI = &get_instance();

        /* Load the config */
        $this->_objCI->load->config('table');
        
        /* Load language */
        $this->_objCI->lang->load('table');

        /* Do we need to load any CSS */
        $css = $this->_objCI->config->item('table_css');
        if(!empty($css)) { $this->_objCI->load->ui('css', $css); }

        /* Do we need to load any JS */
        $js = $this->_objCI->config->item('table_js');
        if(!empty($js)) { $this->_objCI->load->ui('js', $css); }
    }






    /**
     * Generate
     *
     * Generates a table in the ordinary manner. If
     * the $zebra is set as true, it adds an odd/even
     * class to alternate rows of the table so each
     * row can be styled in the zebra fashion.
     *
     * @param mixed $table_data
     * @param bool $zebra
     * @return string
     */
    public function generate($table_data = NULL, $zebra = false) {
        if($zebra === true) {
            $this->set_template(array(
                'row_start' => '<tr class="'.$this->_objCI->config->item('table_row_odd_class').'">',
                'row_alt_start' => '<tr class="'.$this->_objCI->config->item('table_row_even_class').'">'
            ));
        }

        /* Is it an empty table */
        if(!empty($table_data)) {
            /* No - ge the table */
            $table = parent::generate($table_data);
        } else {
            /* Yes - return the empty table message */
            $table = null;
        }

        /* Destroy everything so far so we don't get any conflicts */
        $this->clear();

        /* Return the HTML */
        return $table;
    }






    /**
     * Build
     *
     * Builds a table from an associative array and sets
     * the array element names as the header.  This is
     * designed so that you can pass an array from the
     * database class to this.
     *
     * @param array $arrData
     * @param bool $zebra
     * @param mixed $checkbox
     * @return string
     */
    public function build($arrData = null, $zebra = false, $checkbox = false) {

        $arrHeader = array();
        $arrTable = array();
        $arrSelects = array();

        /* If the $checkbox is an array, assume that it's select data for the checkboxes */
        if(is_array($checkbox)) {
            $arrSelects = $checkbox;
            $checkbox = true;
        }

        if(is_array($arrData) && count($arrData) > 0) {
            foreach($arrData as $i => $item) {
                /* Build the headers */
                $x = 0;
                foreach($item as $key => $value) {
                    /* Only do the top line - no point repeating ourselves */
                    if($i == 0) {
                        if($checkbox && $x == 0) {
                            /* If checkbox and first row, replace with 'Select All' */
                            $arrHeader[] = '<a href="#" class="table_select_all">'.$this->_objCI->lang->line('table_select_all').'</a>';
                        } else {
                            /* If key begins '_', hide it */
                            if(preg_match('/^\_(.+)$/', $key)) { $key = ''; }

                            if($this->_translate !== false) {
                                if(is_bool($this->_translate)) { $key = $this->_objCI->lang->line($key, array(), true); }
                                else { $key = $this->_objCI->lang->line($this->_translate.$key, array(), true); }
                            }
                            $arrHeader[] = $key;
                        }
                        $x++;
                    }
                }

                if($checkbox) {
                    /* Replace first one with a checkbox */
                    $id = key($item);
                    $data = current($item);

                    $item[$id] = form_checkbox("{$id}[]", $data, in_array($data, $arrSelects), ' id="table_'.$id.'" class="table_checkbox"');
                }

                /* Build the rows */
                $arrTable[$i] = $item;
            }
        }

        /* Set the headers if nothing set previously */
        if(empty($this->heading)) { $this->set_heading($arrHeader); }
        
        /* Return the HTML */
        return $this->generate($arrTable, $zebra);
        
    }
    
}

?>